home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / mpeg / parseblock.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  11KB  |  460 lines

  1. /*
  2.  * Copyright (c) 1992 The Regents of the University of California.
  3.  * All rights reserved.
  4.  * 
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  * 
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  * 
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21. #define NO_SANITY_CHECKS
  22. #include <assert.h>
  23. #include "video.h"
  24. #include "proto.h"
  25. #include "decoders.h"
  26.  
  27. /* External declarations. */
  28.  
  29. extern int zigzag_direct[];
  30.  
  31. /* Macro for returning 1 if num is positive, -1 if negative, 0 if 0. */
  32.  
  33. #define Sign(num) ((num > 0) ? 1 : ((num == 0) ? 0 : -1))
  34.  
  35.  
  36. /*
  37.  *--------------------------------------------------------------
  38.  *
  39.  * ParseReconBlock --
  40.  *
  41.  *    Parse values for block structure from bitstream.
  42.  *      n is an indication of the position of the block within
  43.  *      the macroblock (i.e. 0-5) and indicates the type of 
  44.  *      block (i.e. luminance or chrominance). Reconstructs
  45.  *      coefficients from values parsed and puts in 
  46.  *      block.dct_recon array in vid stream structure.
  47.  *      sparseFlag is set when the block contains only one
  48.  *      coeffictient and is used by the IDCT.
  49.  *
  50.  * Results:
  51.  *    
  52.  *
  53.  * Side effects:
  54.  *      Bit stream irreversibly parsed.
  55.  *
  56.  *--------------------------------------------------------------
  57.  */
  58.  
  59. #define DCT_recon blockPtr->dct_recon
  60. #define DCT_dc_y_past blockPtr->dct_dc_y_past
  61. #define DCT_dc_cr_past blockPtr->dct_dc_cr_past
  62. #define DCT_dc_cb_past blockPtr->dct_dc_cb_past
  63.  
  64. #define DECODE_DCT_COEFF_FIRST DecodeDCTCoeffFirst
  65. #define DECODE_DCT_COEFF_NEXT DecodeDCTCoeffNext
  66.  
  67. void
  68. ParseReconBlock(n)
  69.      int n;
  70. {
  71. #ifdef RISC
  72.   unsigned int temp_curBits;
  73.   int temp_bitOffset;
  74.   int temp_bufLength;
  75.   unsigned int *temp_bitBuffer;
  76. #endif
  77.  
  78.   Block *blockPtr = &curVidStream->block;
  79.   int coeffCount;
  80.   
  81.   if (bufLength < 100)
  82.     correct_underflow();
  83.  
  84. #ifdef RISC
  85.   temp_curBits = curBits;
  86.   temp_bitOffset = bitOffset;
  87.   temp_bufLength = bufLength;
  88.   temp_bitBuffer = bitBuffer;
  89. #endif
  90.  
  91.   {
  92.     /*
  93.      * Copy the globals curBits, bitOffset, bufLength, and bitBuffer
  94.      * into local variables with the same names, so the macros use the
  95.      * local variables instead.  This allows register allocation and
  96.      * can provide 1-2 fps speedup.  On machines with not so many registers,
  97.      * don't do this.
  98.      */
  99. #ifdef RISC
  100.     register unsigned int curBits = temp_curBits;
  101.     register int bitOffset = temp_bitOffset;
  102.     register int bufLength = temp_bufLength;
  103.     register unsigned int *bitBuffer = temp_bitBuffer;
  104. #endif
  105.  
  106.     int diff;
  107.     int size, level, i, run, pos, coeff;
  108.     short int *reconptr;
  109.     unsigned char *iqmatrixptr, *niqmatrixptr;
  110.     int qscale;
  111.  
  112.     reconptr = DCT_recon[0];
  113.  
  114.     /* 
  115.      * Hand coded version of memset that's a little faster...
  116.      * Old call:
  117.      *    memset((char *) DCT_recon, 0, 64*sizeof(short int));
  118.      */
  119.     {
  120.       INT32 *p;
  121.       p = (INT32 *) reconptr;
  122.  
  123.       p[0] = p[1] = p[2] = p[3] = p[4] = p[5] = p[6] = p[7] = p[8] = p[9] = 
  124.       p[10] = p[11] = p[12] = p[13] = p[14] = p[15] = p[16] = p[17] = p[18] =
  125.       p[19] = p[20] = p[21] = p[22] = p[23] = p[24] = p[25] = p[26] = p[27] =
  126.       p[28] = p[29] = p[30] = p[31] = 0;
  127.  
  128.     }
  129.  
  130.     if (curVidStream->mblock.mb_intra) {
  131.  
  132.       if (n < 4) {
  133.  
  134.     /*
  135.      * Get the luminance bits.  This code has been hand optimized to
  136.      * get by the normal bit parsing routines.  We get some speedup
  137.      * by grabbing the next 16 bits and parsing things locally.
  138.      * Thus, calls are translated as:
  139.      *
  140.      *    show_bitsX  <-->   next16bits >> (16-X)
  141.      *    get_bitsX   <-->   val = next16bits >> (16-flushed-X);
  142.      *               flushed += X;
  143.      *               next16bits &= bitMask[flushed];
  144.      *    flush_bitsX <-->   flushed += X;
  145.      *               next16bits &= bitMask[flushed];
  146.      *
  147.      * I've streamlined the code a lot, so that we don't have to mask
  148.      * out the low order bits and a few of the extra adds are removed.
  149.      *    bsmith
  150.      */
  151.     unsigned int next16bits, index, flushed;
  152.  
  153.     show_bits16(next16bits);
  154.     index = next16bits >> (16-7);
  155.     size = dct_dc_size_luminance[index].value;
  156.     flushed = dct_dc_size_luminance[index].num_bits;
  157.     next16bits &= bitMask[16+flushed];
  158.  
  159.     if (size != 0) {
  160.       flushed += size;
  161.       diff = next16bits >> (16-flushed);
  162.           if (!(diff & bitTest[32-size])) {
  163.         diff = rBitMask[size] | (diff + 1);
  164.       }
  165.     } else {
  166.       diff = 0;
  167.     }
  168.     flush_bits(flushed);
  169.  
  170.     if (n == 0) {
  171.       coeff = diff << 3;
  172.       if (curVidStream->mblock.mb_address -
  173.           curVidStream->mblock.past_intra_addr > 1) 
  174.         coeff += 1024;
  175.       else coeff += DCT_dc_y_past;
  176.       DCT_dc_y_past = coeff;
  177.     } else {
  178.       coeff = DCT_dc_y_past + (diff << 3);
  179.       DCT_dc_y_past = coeff;
  180.     }
  181.       } else {
  182.     
  183.     /*
  184.      * Get the chrominance bits.  This code has been hand optimized to
  185.      * as described above
  186.      */
  187.     unsigned int next16bits, index, flushed;
  188.  
  189.     show_bits16(next16bits);
  190.     index = next16bits >> (16-8);
  191.     size = dct_dc_size_chrominance[index].value;
  192.     flushed = dct_dc_size_chrominance[index].num_bits;
  193.     next16bits &= bitMask[16+flushed];
  194.     
  195.     if (size != 0) {
  196.       flushed += size;
  197.       diff = next16bits >> (16-flushed);
  198.           if (!(diff & bitTest[32-size])) {
  199.         diff = rBitMask[size] | (diff + 1);
  200.       }
  201.     } else {
  202.       diff = 0;
  203.     }
  204.     flush_bits(flushed);
  205.     
  206.     if (n == 4) {
  207.       coeff = diff << 3;
  208.       if (curVidStream->mblock.mb_address -
  209.           curVidStream->mblock.past_intra_addr > 1) 
  210.         coeff += 1024;
  211.       else coeff += DCT_dc_cr_past;
  212.       DCT_dc_cr_past = coeff;
  213.  
  214.     } else {
  215.       coeff = diff << 3;
  216.       if (curVidStream->mblock.mb_address -
  217.           curVidStream->mblock.past_intra_addr > 1) 
  218.         coeff += 1024;
  219.       else coeff += DCT_dc_cb_past;
  220.       DCT_dc_cb_past = coeff;
  221.     }
  222.       }
  223.       
  224.       *reconptr = coeff;
  225.       i = 0; pos = 0;
  226.       coeffCount = (coeff != 0);
  227.     
  228.       if (curVidStream->picture.code_type != 4) {
  229.     
  230.     qscale = curVidStream->slice.quant_scale;
  231.     iqmatrixptr = curVidStream->intra_quant_matrix[0];
  232.     
  233.     while(1) {
  234.       
  235.       DECODE_DCT_COEFF_NEXT(run, level);
  236.  
  237.       if (run == END_OF_BLOCK) break;
  238.  
  239.       i = i + run + 1;
  240.       pos = zigzag_direct[i];
  241.       coeff = (level * qscale * ((int) iqmatrixptr[pos])) >> 3;
  242.       if (level < 0) {
  243.           coeff += (coeff & 1);
  244.       } else {
  245.           coeff -= (coeff & 1);
  246.       }
  247.  
  248.       reconptr[pos] = coeff;
  249.       if (coeff) {
  250.         coeffCount++;
  251.       }
  252.  
  253.     }
  254.  
  255. #ifdef ANALYSIS 
  256.  
  257.     {
  258.       extern unsigned int *mbCoeffPtr;
  259.       mbCoeffPtr[pos]++;
  260.     }
  261. #endif
  262.  
  263.     flush_bits(2);
  264.  
  265.     goto end;
  266.       }
  267.     }
  268.     
  269.     else {
  270.       
  271.       niqmatrixptr = curVidStream->non_intra_quant_matrix[0];
  272.       qscale = curVidStream->slice.quant_scale;
  273.       
  274.       DECODE_DCT_COEFF_FIRST(run, level);
  275.       i = run;
  276.  
  277. /** Added TC ***/
  278.       if( i > 63)
  279.       {
  280.      fprintf(stderr,"Something is wrong in %s %d\n",__FILE__, __LINE__);
  281.      longjmp(errjmp, 1);
  282.       }
  283.       pos = zigzag_direct[i];
  284.       if (level < 0) {
  285.       coeff = (((level<<1) - 1) * qscale * 
  286.            ((int) (niqmatrixptr[pos]))) >> 4; 
  287.       coeff += (coeff & 1);
  288.       } else {
  289.       coeff = (((level<<1) + 1) * qscale * 
  290.            ((int) (*(niqmatrixptr+pos)))) >> 4; 
  291.       coeff -= (coeff & 1);
  292.       }
  293.       reconptr[pos] = coeff;
  294.       if (coeff) {
  295.     coeffCount = 1;
  296.       }
  297.  
  298.       if (curVidStream->picture.code_type != 4) {
  299.     
  300.     while(1) {
  301.       
  302.       DECODE_DCT_COEFF_NEXT(run, level);
  303.  
  304.       if (run == END_OF_BLOCK) break;
  305.  
  306.       i = i+run+1;
  307. /** Added TC ***/
  308.       if( i > 63)
  309.       {
  310.      fprintf(stderr,"Something is wrong in %s %d\n",__FILE__, __LINE__);
  311.      longjmp(errjmp, 1);
  312.       }
  313.       pos = zigzag_direct[i];
  314.       if (level < 0) {
  315.           coeff = (((level<<1) - 1) * qscale * 
  316.                ((int) (niqmatrixptr[pos]))) >> 4; 
  317.           coeff += (coeff & 1);
  318.       } else {
  319.           coeff = (((level<<1) + 1) * qscale * 
  320.                ((int) (*(niqmatrixptr+pos)))) >> 4; 
  321.           coeff -= (coeff & 1);
  322.       }
  323.       reconptr[pos] = coeff;
  324.       if (coeff) {
  325.         coeffCount++;
  326.       }
  327.     }
  328.  
  329. #ifdef ANALYSIS
  330.     {
  331.       extern unsigned int *mbCoeffPtr;
  332.       mbCoeffPtr[pos]++;
  333.     }
  334. #endif
  335.  
  336.     flush_bits(2);
  337.  
  338.     goto end;
  339.       }
  340.     }
  341.     
  342.   end:
  343.  
  344.     if (coeffCount == 1) j_rev_dct_sparse (reconptr, pos);
  345.     else mpeg_j_rev_dct(reconptr);
  346.  
  347. #ifdef RISC
  348.     temp_curBits = curBits;
  349.     temp_bitOffset = bitOffset;
  350.     temp_bufLength = bufLength;
  351.     temp_bitBuffer = bitBuffer;
  352. #endif
  353.  
  354.   }
  355.  
  356. #ifdef RISC
  357.   curBits = temp_curBits;
  358.   bitOffset = temp_bitOffset;
  359.   bufLength = temp_bufLength;
  360.   bitBuffer = temp_bitBuffer;
  361. #endif
  362. }
  363.     
  364. #undef DCT_recon 
  365. #undef DCT_dc_y_past 
  366. #undef DCT_dc_cr_past 
  367. #undef DCT_dc_cb_past 
  368.  
  369.  
  370. /*
  371.  *--------------------------------------------------------------
  372.  *
  373.  * ParseAwayBlock --
  374.  *
  375.  *    Parses off block values, throwing them away.
  376.  *      Used with grayscale dithering.
  377.  *
  378.  * Results:
  379.  *    None.
  380.  *
  381.  * Side effects:
  382.  *      None.
  383.  *
  384.  *--------------------------------------------------------------
  385.  */
  386.  
  387. void
  388. ParseAwayBlock(n)
  389.      int n;
  390. {
  391.   unsigned int diff;
  392.   unsigned int size, run;
  393.   int level;
  394.  
  395.   if (bufLength < 100)
  396.     correct_underflow();
  397.  
  398.   if (curVidStream->mblock.mb_intra) {
  399.  
  400.     /* If the block is a luminance block... */
  401.  
  402.     if (n < 4) {
  403.  
  404.       /* Parse and decode size of first coefficient. */
  405.  
  406.       DecodeDCTDCSizeLum(size);
  407.  
  408.       /* Parse first coefficient. */
  409.  
  410.       if (size != 0) {
  411.     get_bitsn(size, diff);
  412.       }
  413.     }
  414.  
  415.     /* Otherwise, block is chrominance block... */
  416.  
  417.     else {
  418.  
  419.       /* Parse and decode size of first coefficient. */
  420.  
  421.       DecodeDCTDCSizeChrom(size);
  422.  
  423.       /* Parse first coefficient. */
  424.  
  425.       if (size != 0) {
  426.     get_bitsn(size, diff);
  427.       }
  428.     }
  429.   }
  430.  
  431.   /* Otherwise, block is not intracoded... */
  432.  
  433.   else {
  434.  
  435.     /* Decode and set first coefficient. */
  436.  
  437.     DECODE_DCT_COEFF_FIRST(run, level);
  438.   }
  439.  
  440.   /* If picture is not D type (i.e. I, P, or B)... */
  441.  
  442.   if (curVidStream->picture.code_type != 4) {
  443.  
  444.     /* While end of macroblock has not been reached... */
  445.  
  446.     while (1) {
  447.  
  448.       /* Get the dct_coeff_next */
  449.  
  450.       DECODE_DCT_COEFF_NEXT(run, level);
  451.  
  452.       if (run == END_OF_BLOCK) break;
  453.     }
  454.  
  455.     /* End_of_block */
  456.  
  457.     flush_bits(2);
  458.   }
  459. }
  460.